package com.code.servlet;

import com.alibaba.fastjson.JSON;
import com.code.pojo.User;
import com.code.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

@WebServlet("/test03Servlet")
public class Test03Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            User user = JSON.parseObject(request.getInputStream(),User.class);
            //2. 业务处理

            User u1 = new User("zhangsan", "18");
            User u2 = new User("lisi", "19");
            User u3 = new User("wangwu", "20");
            ArrayList<User> list = new ArrayList<>();
            Collections.addAll(list,u1,u2,u3);

            //3. 响应数据
            Result result = new Result(true, "获取好友列表成功", list);
            response.setContentType("application/json;charset=utf-8");
            String json = JSON.toJSONString(result);
            response.getWriter().print(json);
        } catch (Exception e) {
            Result result = new Result(false, "获取好友列表失败");
            response.setContentType("application/json;charset=utf-8");
            String json = JSON.toJSONString(result);
            response.getWriter().print(json);
        }
    }
}
